package com.ruoyi.project.erp.checkstock.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.OddUtil;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.project.erp.checkstocks.domain.Checkstocks;
import com.ruoyi.project.erp.checkstocks.service.ICheckstocksService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.erp.checkstock.domain.Checkstock;
import com.ruoyi.project.erp.checkstock.service.ICheckstockService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2020-09-08
 */
@Controller
@RequestMapping("/erp/checkstock")
public class CheckstockController extends BaseController
{
    private String prefix = "erp/checkstock";

    @Autowired
    private ICheckstockService checkstockService;
    @Autowired
    private ICheckstocksService checkstocksService;

    @RequiresPermissions("erp:checkstock:view")
    @GetMapping()
    public String checkstock()
    {
        return prefix + "/checkstock";
    }


    /**
     * 手机端添加库存信息
     * */
    @PostMapping("/addOrder")
    @ResponseBody
    public AjaxResult addOrder(Date ddate, String emp, String shop, String shopName, @RequestBody JSONObject json)
    {
        List<Checkstocks> checkstocksList = json.getJSONArray("checkstocksList").toJavaList(Checkstocks.class);
        Checkstock checkstock = new Checkstock();
        String mcode = OddUtil.createOdd("",checkstockService.selectMaxCode());
        checkstock.setCode(mcode);
        checkstock.setDdate(ddate);
        checkstock.setEmp(emp);
        checkstock.setShop(shop);
        checkstock.setShopname(shopName);
        checkstock.setSt("保存");
        int num = checkstockService.insertCheckstock(checkstock);
        String mid = checkstockService.selectMid();
        if(num>0){
            for(int i=0;i<checkstocksList.size();i++){
                Checkstocks temp = checkstocksList.get(i);
                temp.setMid(Long.parseLong(mid));
                temp.setMcode(mcode);
                checkstocksService.insertCheckstocks(temp);
            }
        }
        return toAjax(num);
    }

    /**
     * 手机端查询盘存列表
     */
    @PostMapping("/listOrder")
    @ResponseBody
    public AjaxResult listOrder(@RequestBody Checkstock checkstock,String permit)
    {
        //查询当前用户的权限
      /*  String[] emps = permit.split(",");
        List<Checkstock> resultList = new ArrayList<>();
        for(int i=0;i<emps.length;i++){
            checkstock.setEmp(emps[i]);
            resultList.addAll(checkstockService.selectCheckstockList(checkstock));
        }*/
        return new AjaxResult(AjaxResult.Type.SUCCESS,"200",checkstockService.selectCheckstockListPermit(checkstock,permit));
    }

    /**
     * 手机端查询盘存单列表
     */
    @PostMapping("/listOrderQuery")
    @ResponseBody
    public AjaxResult listOrderQuery(@RequestBody Checkstock checkstock,String beginTime,String endTime,String permit)
    {
        //查询当前用户的权限
       /* String[] emps = permit.split(",");
        List<Checkstock> resultList = new ArrayList<>();
        for(int i=0;i<emps.length;i++) {
            checkstock.setEmp(emps[i]);
            resultList.addAll(checkstockService.selectCheckStockListQuery(checkstock, beginTime, endTime));
        }*/
        return new AjaxResult(AjaxResult.Type.SUCCESS,"200",checkstockService.selectCheckStockListQuery(checkstock, beginTime, endTime,permit));
    }

    /**
     * 手机端根据盘存单ID查询订单详情
     */
    @PostMapping("/listOrderDetail")
    @ResponseBody
    public AjaxResult listOrderDetail(long orderId)
    {
        Checkstocks checkstocks = new Checkstocks();
        checkstocks.setMid(orderId);
        List<Checkstocks> list = checkstocksService.selectCheckstocksList(checkstocks);
        return new AjaxResult(AjaxResult.Type.SUCCESS,"200",list);
    }

    /**
     * 审批
     */
    @RequiresPermissions("erp:checkstock:edit")
    @PostMapping("/reviewCheckStock")
    @ResponseBody
    public AjaxResult reviewCheckStock(Long id)
    {
        //根据id获取施工单信息
        Checkstock temp =new Checkstock();
        temp.setCreditemp(getSysUser().getF102());
        temp.setCreditdt(new Date());
        temp.setId(id);
        temp.setSt("审核");
        return toAjax(checkstockService.updateCheckstock(temp));
    }
    /**
     * 撤销审批
     */
    @RequiresPermissions("erp:checkstock:edit")
    @PostMapping("/disReviewCheckStock")
    @ResponseBody
    public AjaxResult disReviewCheckStock(Long id)
    {
        //根据id获取施工单信息
        Checkstock temp =new Checkstock();
        temp.setId(id);
        return toAjax(checkstockService.disReviewCheckstock(temp));
    }

    /**
     * PC端查询列表
     */
    @RequiresPermissions("erp:checkstock:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Checkstock checkstock)
    {
        Integer pageNum = ServletUtils.getParameterToInt(Constants.PAGE_NUM);
        Integer pageSize =ServletUtils.getParameterToInt(Constants.PAGE_SIZE);
        List<Checkstock> list = checkstockService.selectCheckstockList(checkstock);
        List<Checkstock> resultList = new ArrayList<>();
        int totalPage = list.size()/pageSize+1;
        int count = pageNum*pageSize;
        if (pageNum == totalPage){
            count = list.size();
        }
        for(int start = (pageNum-1)*pageSize;start<count;start++){
            resultList.add(list.get(start));
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(resultList);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    /**
     * PC查询子列表
     */
    @RequiresPermissions("erp:checkstock:list")
    @PostMapping("/listChild")
    @ResponseBody
    public TableDataInfo listChild(long mid)
    {
        Checkstocks checkstocks = new Checkstocks();
        checkstocks.setMid(mid);
        List<Checkstocks> childList =checkstocksService.selectCheckstocksList(checkstocks);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(childList);
        return rspData;
    }

    /**
     * PC根据id获取订单
     */
    @RequiresPermissions("erp:checkstock:list")
    @PostMapping("/getCheckStockById")
    @ResponseBody
    public AjaxResult getCheckStockById(long id)
    {
        Checkstock checkstock  = checkstockService.selectCheckstockById(id);
        return new AjaxResult(AjaxResult.Type.SUCCESS,"200",checkstock);
    }


    /**
     * 导出主子列表
     */
    @RequiresPermissions("erp:setupbill:export")
    @PostMapping("/exportQuery")
    @ResponseBody
    public AjaxResult exportQuery(long[] rowsId)
    {
        List<Checkstock> resultList = new ArrayList();
        List<Checkstock> list = checkstockService.queryCheckstockList(rowsId);
        for(int i=0;i<list.size();i++){
            Checkstocks checkstocks = new Checkstocks();
            checkstocks.setMid(list.get(i).getId());
            List<Checkstocks> setupbillsList = checkstocksService.selectCheckstocksList(checkstocks);
            for(int j=0;j<setupbillsList.size();j++){
                //Setupbill temp = list.get(i);
                Checkstock temp = new Checkstock();
                temp.setId(list.get(i).getId());
                temp.setSt(list.get(i).getSt());
                temp.setCode(list.get(i).getCode());
                temp.setCreditdt(list.get(i).getCreditdt());
                temp.setCreditemp(list.get(i).getCreditemp());
                temp.setDdate(list.get(i).getDdate());
                temp.setEmp(list.get(i).getEmp());
                temp.setShopname(list.get(i).getShopname());
                temp.setMemo(list.get(i).getMemo());
                temp.setMcode(setupbillsList.get(j).getMcode());
                if(setupbillsList.get(j).getSmemo() != null){
                    temp.setSmemo(setupbillsList.get(j).getSmemo());
                }
                temp.setPcode(setupbillsList.get(j).getItemcode());
                temp.setPname(setupbillsList.get(j).getPname());
                temp.setPstd(setupbillsList.get(j).getPstd());
                temp.setPunit(setupbillsList.get(j).getPunit());
                temp.setQty(setupbillsList.get(j).getQty());
                resultList.add(temp);
            }
        }
        ExcelUtil<Checkstock> util = new ExcelUtil<Checkstock>(Checkstock.class);
        return util.exportExcel(resultList, "checkstock");
    }

    /**
     * 导出全部主子列表
     */
    @RequiresPermissions("erp:setupbill:export")
    @PostMapping("/exportAll")
    @ResponseBody
    public AjaxResult exportAll(Checkstock checkstock)
    {
        List<Checkstock> resultList = new ArrayList();
        List<Checkstock> list = checkstockService.selectCheckstockList(checkstock);
        for(int i=0;i<list.size();i++){
            Checkstocks checkstocks = new Checkstocks();
            checkstocks.setMid(list.get(i).getId());
            List<Checkstocks> setupbillsList = checkstocksService.selectCheckstocksList(checkstocks);
            for(int j=0;j<setupbillsList.size();j++){
                //Setupbill temp = list.get(i);
                Checkstock temp = new Checkstock();
                temp.setId(list.get(i).getId());
                temp.setSt(list.get(i).getSt());
                temp.setCode(list.get(i).getCode());
                temp.setCreditdt(list.get(i).getCreditdt());
                temp.setCreditemp(list.get(i).getCreditemp());
                temp.setDdate(list.get(i).getDdate());
                temp.setEmp(list.get(i).getEmp());
                temp.setShopname(list.get(i).getShopname());
                temp.setMemo(list.get(i).getMemo());
                temp.setMcode(setupbillsList.get(j).getMcode());
                if(setupbillsList.get(j).getSmemo() != null){
                    temp.setSmemo(setupbillsList.get(j).getSmemo());
                }
                temp.setPcode(setupbillsList.get(j).getItemcode());
                temp.setPname(setupbillsList.get(j).getPname());
                temp.setPstd(setupbillsList.get(j).getPstd());
                temp.setPunit(setupbillsList.get(j).getPunit());
                temp.setQty(setupbillsList.get(j).getQty());
                resultList.add(temp);
            }
        }
        ExcelUtil<Checkstock> util = new ExcelUtil<Checkstock>(Checkstock.class);
        return util.exportExcel(resultList, "checkstock");
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("erp:checkstock:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Checkstock checkstock)
    {
        List<Checkstock> list = checkstockService.selectCheckstockList(checkstock);
        ExcelUtil<Checkstock> util = new ExcelUtil<Checkstock>(Checkstock.class);
        return util.exportExcel(list, "checkstock");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("erp:checkstock:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Checkstock checkstock)
    {
        return toAjax(checkstockService.insertCheckstock(checkstock));
    }

    /**
     * 修改
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        String idStr = String.valueOf(id);
        String pre = idStr.substring(0,2);
        if("11".equals(pre)){
            idStr = idStr.substring(2,idStr.length());
            Long newId = Long.parseLong(idStr);
            Checkstock checkstock = checkstockService.selectCheckstockById(newId);
            mmap.put("checkstock", checkstock);
            return  "erp/checkstock/edit";
        }else{
            idStr = idStr.substring(2,idStr.length());
            Long newId = Long.parseLong(idStr);
            Checkstocks checkstocks = checkstocksService.selectCheckstocksById(newId);
            mmap.put("checkstocks", checkstocks);
            return  "erp/checkstocks/edit";
        }
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("erp:checkstock:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Checkstock checkstock)
    {
        return toAjax(checkstockService.updateCheckstock(checkstock));
    }

    /**
     * 删除【请填写功能名称】
     */
    //@RequiresPermissions("erp:checkstock:remove")
    //@Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        Checkstock checkstock = checkstockService.selectCheckstockById(Long.parseLong(ids));
        if(!"保存".equals(checkstock.getSt())){
            return error("订单已审核，不能删除!");
        }
        return toAjax(checkstockService.deleteCheckstockByIds(ids));
    }
}
